<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" class="!w-240px" clearable placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="姓名" prop="nickname">
        <el-input v-model="queryParams.nickname" class="!w-240px" clearable placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="状态" prop="status" class="!w-240px" clearable placeholder="选择状态">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="姓名" prop="nickname" />
      <el-table-column align="center" label="手机号" min-width="90" prop="userMobile" />
      <el-table-column align="center" label="身份证号" min-width="90" prop="idNumber" />
      <el-table-column align="center" label="身份证正面" min-width="90" prop="idNumberFrontImage">
        <template #default="scope">
          <el-image v-if="scope.row.idNumberFrontImage" lazy class="h-64px w-64px shrink-0 rounded-4px"
            preview-teleported hide-on-click-modal :src="scope.row.idNumberFrontImage"
            :preview-src-list="[scope.row.idNumberFrontImage]" />
          <span v-else class="text-red-500">未上传</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="身份证背面" min-width="90" prop="idNumberBackImage">
        <template #default="scope">
          <el-image v-if="scope.row.idNumberBackImage" lazy class="h-64px w-64px shrink-0 rounded-4px"
            preview-teleported hide-on-click-modal :src="scope.row.idNumberBackImage"
            :preview-src-list="[scope.row.idNumberBackImage]" />
          <span v-else class="text-red-500">未上传</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="身份证有效期" min-width="90" prop="certBeginDate">
        <template #default="scope">
          {{ scope.row.certBeginDate ? `${scope.row.certBeginDate} - ${scope.row.certEndDate ?
            scope.row.certEndDate : '长期'}` : '' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="银行卡号" min-width="90" prop="bankCardNumber" />
      <el-table-column align="center" label="银行卡属地" min-width="90" prop="provinceName">
        <template #default="scope">
          {{ scope.row.provinceName }} {{ scope.row.cityName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="银行卡照片" min-width="90" prop="bankCardImage">
        <template #default="scope">
          <el-image v-if="scope.row.bankCardImage" lazy class="h-64px w-64px shrink-0 rounded-4px" preview-teleported
            hide-on-click-modal :src="scope.row.bankCardImage" :preview-src-list="[scope.row.bankCardImage]" />
          <span v-else class="text-red-500">未上传</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="审核状态" min-width="90" prop="status">
        <template #default="scope">
          <span v-if="scope.row.status == -1" class="text-blue-500">未认证</span>
          <span v-if="scope.row.status == 0" class="text-yellow-500">待审核</span>
          <span v-if="scope.row.status == 1" class="text-green-500">已通过</span>
          <span v-if="scope.row.status == 2" class="text-red-500">已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" min-width="110">
        <template #default="scope">
          <el-button v-if="scope.row.status == 0" link type="primary" @click="handlerPass(scope.row.id)"> 通过
          </el-button>
          <el-button v-if="scope.row.status == 0" link type="danger" @click="handlerReject(scope.row.id)"> 拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
      @pagination="getList" />
  </ContentWrap>
</template>

<script setup lang="ts">
import * as WorkerAuthApi from '@/api/pages/workerAuth'
defineOptions({ name: 'WorkerAuth' })
const message = useMessage() // 消息弹窗

onMounted(async () => {
  await getList()
})

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  mobile: undefined,
  nickname: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const statusOptions = [
  { label: '待审核', value: '0' },
  { label: '已通过', value: '1' },
  { label: '已拒绝', value: '2' }
]
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WorkerAuthApi.getPageMemberUserRole(queryParams)

    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  console.log(queryFormRef.value);

  handleQuery()
}
// 通过审核
const handlerPass = async (id: string) => {
  try {
    await message.confirm(`您确定通过审核吗？`)
    await WorkerAuthApi.updateRoleStatus({ userId: id, status: 1 })
    message.success('操作成功')
    getList()
  } catch {
    console.log("取消");
  }
}
// 拒绝审核
const handlerReject = async (id: string) => {
  try {
    await message.confirm(`您确定通过审核吗？`)
    await WorkerAuthApi.updateRoleStatus({ userId: id, status: 2 })
    message.success('操作成功')
    getList()
  } catch {
    console.log("取消");
  }
}
</script>

<style lang="scss" scoped></style>
